package es.ats.codington.festival.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventService;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//Visitor visitor = new Visitor();	
		HttpSession sesion = request.getSession(false);
		
		if (sesion == null)
		{
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/");
	        requestDispatcher.forward(request, response);
		}
		else
		{
			try
			{
				boolean  adminvisitor = (Boolean) sesion.getAttribute("isadmin");
				if (adminvisitor == true)
				{
					EventService es=new EventService();
					List<Event> events = new ArrayList<Event>();
					events=es.getAllEvents();
					request.setAttribute("allevents",events);
					request.setAttribute("eventsize",events.size());
					RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/adminEvents.jsp");
				    requestDispatcher.forward(request, response);
				}
				/*else
				{
					RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/home.jsp");
			        requestDispatcher.forward(request, response);
				}*/
			}
			catch (Exception e) {
				request.setAttribute("error", "Something is wrong with your data. Please, try it again in a few minutes.");
				RequestDispatcher rd = request.getRequestDispatcher("jsp/error.jsp");
				rd.forward(request,response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
